//
//  PushBehaviorView.swift
//  Dynamic
//
//  Created by Yinyin Zeng on 30/5/20.
//  Copyright © 2020 MaoYeYeDeJiangCuCha. All rights reserved.
//

import UIKit

class PushBehaviorView: UIView {
    
    let squareView = UIImageView()
    var animator = UIDynamicAnimator()//需要强引用
    
    override init(frame: CGRect) {
        super.init(frame:frame)
        squareView.image = UIImage(named: "Box1")
        squareView.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
        self.addSubview(squareView)
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        //创建动画对象
        animator = UIDynamicAnimator(referenceView: self)
        //推行为
        // continuous       持续推力
        // instantaneous
        let pushBehavior = UIPushBehavior(items: [self.squareView], mode: .continuous)
        //量级 - 推多大力 必须设置 否则无效
        pushBehavior.magnitude = 10
        //设置方向 - 角度
//        pushBehavior.angle = CGFloat(M_2_PI)
        //设置方向 - 向量
        pushBehavior.pushDirection = CGVector(dx: 20, dy: 20)
        
        //把行为添加到动画中
        animator.addBehavior(pushBehavior)
        
    }
    
    

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
